package com.party.mobile.web.controller.mall;

import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.model.system.TargetType;
import com.party.mobile.biz.mall.MallCategoryBizService;
import com.party.mobile.web.annotation.VisitCollect;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.mall.output.MallCategoryOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 商城分类控制器
 * @Author: yangshoukun
 * @Date: 2018/4/16 16:08
 */
@Controller
@RequestMapping("mall/category")
public class MallCategoryController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MallCategoryBizService mallCategoryBizService;

    /**
     * 获取商城一级分类列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @VisitCollect(module = TargetType.MALL)
    public AjaxResult list() {
        return mallCategoryBizService.list();
    }

    /**
     * 获取二级分类列表
     * @param id: 一级分类id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public AjaxResult getDetail(String id) {
        return mallCategoryBizService.getDetail(id);
    }

    /**
     * 分类筛选面板数据
     * @param id: 二级分类id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/siftData", method = RequestMethod.GET)
    public AjaxResult siftData(String id) {
        return mallCategoryBizService.siftData(id);
    }


    /**
     * 根据分类id获取分类信息
     *
     * @param id 分类id
     * @return
     */
    @ResponseBody
    @RequestMapping("findById")
    public AjaxResult findById(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "id不能为空");
        }
        try {
            MallCategoryOutput output = mallCategoryBizService.findById(id);
            return AjaxResult.success(output);
        } catch (Exception e) {
            logger.error("根据id获取分类异常", e);
            return AjaxResult.error(100, "根据id获取分类异常");
        }
    }
}
